package com.itheima.controller;

import com.itheima.dto.ChatDTO;
import com.itheima.tools.MobileTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private ChatClient chatClient;

    @Autowired
    private MobileTools mobileTools;


    @PostMapping(value = "/chat",produces ="text/html;charset=UTF-8" )
    public Flux<String> chat(@RequestBody ChatDTO dto){
        return chatClient.prompt()
                .user(dto.getQuestion())
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, dto.getSessionId()))
                .tools(mobileTools)
                .stream()
                .content()
                .concatWith(Flux.just("[END]"));
    }
}
